<?php
namespace app\office\controller;

use think\Request;
use think\Db;
use app\office\controller\OfficeBase;
use app\office\model\Position as PositionModel;
use app\office\model\ModuleStatic as ModuleStaticModel;

class PositionModuleStatic extends OfficeBase
{ 
    protected $position_filter=[];
    protected $module_static_filter=[];
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
      //验证Cookie↓
      $this->checkCookie();
      // 引入navbar
      $this->setNavbar();
      $this->assign('title',"职位工程师模块操作阶段列表");
      if(!input('?request.')){
        $data=input('request.');
        foreach($data as $k=>$v){
          if($v!=null& in_array($k,['position_is_sth','position_sth_num','position_static'])){     /* 使用in_array($k,['is_sth','sth_num','static'],是因为paginate传参是get方法,需要筛选变量 */
            $k=str_replace("position_","",$k);
            $this->position_filter[$k]=$v;
          }
        }
      }
      // $position_filter_msg=$this->position_filter;
      $position=PositionModel::scope('static')->where($this->position_filter)->order('id','desc')->select();   /* 这里用的是查询构建器的select()方法;或者可以使用模型的all()方法,$list=PositionModel::all(); */
      foreach($position as $k=>$v){
        $position_list[$v->id]=$v;
        $position_list[$v->id]['module_static']=$v->module_static;
      }
      $countall=PositionModel::scope('static')->where($this->position_filter)->count();
      $this->assign('position_list',$position_list);
      $this->assign('countall',$countall);
      return $this->fetch();
    }
    
    /**
     * 显示资源列表(模块操作阶段为主体)
     *
     * @return \think\Response
     */
    public function index_module_static()
    {
      //验证Cookie↓
      $this->checkCookie();
      // 引入navbar
      $this->setNavbar();
      $this->assign('title',"模块操作阶段-职位工程师列表");        
      if(!input('?request.')){
        $data=input('request.');
        foreach($data as $k=>$v){
          if($v!=null& in_array($k,['module_static_is_sth','module_static_sth_num','module_static_static'])){     /* 使用in_array($k,['is_sth','sth_num','static'],是因为paginate传参是get方法,需要筛选变量 */
            $k=str_replace("module_static_","",$k);
            $this->module_static_filter[$k]=$v;
          }
        }
      }
      // $position_filter_msg=$this->position_filter;
      $module_static=ModuleStaticModel::scope('static')->where($this->module_static_filter)->order('id','desc')->select();   /* 这里用的是查询构建器的select()方法;或者可以使用模型的all()方法,$list=PositionModel::all(); */
      foreach($module_static as $k=>$v){
        $module_static_list[$v->id]=$v;
        $module_static_list[$v->id]['module_static']=$v->position;
      }
      $countall=ModuleStaticModel::scope('static')->where($this->module_static_filter)->count();
      $this->assign('module_static_list',$module_static_list);
      $this->assign('countall',$countall);
      return $this->fetch();
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
      //验证Cookie↓
      $this->checkCookie();
      // 引入navbar
      $this->setNavbar();
      $this->assign('title',"职位工程师模块操作阶段分配(批量)"); 
      // 此处根据是否有搜索信息来进行筛选
      if(!input('?request.')){
        $data=input('request.');
        foreach($data as $k=>$v){
          if($v!=null& in_array($k,['position_is_sth','position_sth_num','position_static'])){     /* 使用in_array($k,['is_sth','sth_num','static'],是因为paginate传参是get方法,需要筛选变量 */
            $k=str_replace("position_","",$k);
            $this->position_filter[$k]=$v;
          }
          if($v!=null& in_array($k,['module_static_is_sth','module_static_sth_num','module_static_static'])){     /* 使用in_array($k,['is_sth','sth_num','static'],是因为paginate传参是get方法,需要筛选变量 */
            $k=str_replace("module_static_","",$k);
            $this->module_static_filter[$k]=$v;
          }
        }
      }
      // 此处是获取position清单
      //注:此处使用了scope('static')后,不会显示static=0,即'删除'的数据
      $list=PositionModel::scope('static')->where($this->position_filter)->order('id','desc')->select();   /* 这里用的是查询构建器的select()方法;或者可以使用模型的all()方法,$list=PositionModel::all(); */
      $countall=PositionModel::scope('static')->where($this->position_filter)->count();
      $this->assign('position_list',$list);
      $this->assign('position_countall',$countall);
      // 此处是获取module_static清单
      $list=ModuleStaticModel::scope('static')->where($this->module_static_filter)->order('id','desc')->select();   /* 这里用的是查询构建器的select()方法;或者可以使用模型的all()方法,$list=ModuleStaticModel::all(); */
      $countall=ModuleStaticModel::scope('static')->where($this->module_static_filter)->count();
      $this->assign('module_static_list',$list);
      $this->assign('module_static_countall',$countall);
      return $this->fetch();
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
      $msg=array();
      $data=input('request.');
      if(empty($data['select_position'])){
        $this->error("请选择职位工程师",'create','',1);
      }
      $select_position_id=$data['select_position'];
      if(empty($data['select_module_static'])){
        $this->error("请选择模块操作阶段",'create','',1);
      }
      $select_module_static_id=$data['select_module_static'];
      
      // 上面的操作完了,该准备写入的json了
      // 此处获取要写入的员工code的json
      // foreach($select_position_id as $emp_id){
        // $position=PositionModel::getById($emp_id);
        // $employee=$position->employee;
        // foreach($employee as $k=>$v){
          // $temp[$v->code]=$v->title;
        // }
      // }
      // $process_id=json_encode(array_keys($temp));
      // 此处循环写入模块的可操作人员process_id中
      // foreach($select_module_static_id as $org_id){
          // $module_static=ModuleStaticModel::getById($org_id);
          // $module_static->process_id=$process_id;
          // if(!$module_static->save()){
            // $this->error('数据出现错误,请联系管理员','index','',1);
          // }
      // }
      //阶段性部分↑
      
      // dump($select_module_static);exit;
      foreach($select_position_id as $emp_id){
        $position=PositionModel::getById($emp_id);
        // 取出$position已经写入数据库的org_id;
        $org_id_in_database=Db::name('position_module_static')->field('module_static_id')->where('position_id',$emp_id)->select();
        // 给查询出的数据降维
        foreach($org_id_in_database as $k=>$v){
          $org_id_in_database[$k]=$v['module_static_id'];
        }
        $select_module_static_id=array_diff($select_module_static_id,$org_id_in_database);
        if(empty($select_module_static_id)){
          $text="职位工程师:".$position->title."所选模块操作阶段均已分配过";
          array_push($msg,$text);
        }
        foreach($select_module_static_id as $org_id){
          $module_static=ModuleStaticModel::getById($org_id);
          $position->moduleStatic()->attach($module_static);
          $text="职位工程师:".$position->title."模块操作阶段:".$module_static->title."分配完毕";
          array_push($msg,$text);
        }
      }
      dump($msg);
      $this->success("操作完成",'create','',1);
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
      //验证Cookie↓
      $this->checkCookie();
      // 引入navbar
      $this->setNavbar();
      $this->assign('title',"职位工程师模块操作阶段信息");     
      $position=PositionModel::get($id,'module_static');
      $position_module_static=$position->module_static;
      // dump( $position_module_static);
      $this->assign('position',$position);
      $this->assign('position_module_static',$position_module_static);
      return $this->fetch();
      // return view();
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
      //验证Cookie↓
      $this->checkCookie();
      // 引入navbar
      $this->setNavbar();
      $this->assign('title',"职位工程师模块操作阶段编辑");
      $position=PositionModel::getById($id);
      $position_module_static=$position->module_static;
      $this->assign('position',$position);
      $this->assign('position_module_static',$position_module_static);
      $countall=ModuleStaticModel::scope('static')->count();
      $this->assign('countall',$countall);
      $module_static_tree=ModuleStaticModel::getTree();
      $this->assign('module_static_tree',$module_static_tree);
      // dump($module_static_tree);
      return $this->fetch();
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        dump(input('post.'));
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($emp_id,$org_id)
    {
      $position=PositionModel::getById($emp_id);
      $module_static=ModuleStaticModel::getById($org_id);
      $position->moduleStatic()->detach($module_static);
      $this->redirect('read',['id'=>$emp_id]);
    }
}
